import Vue from 'vue'
import VueRouter from 'vue-router'

import work from '@/views/work/view_work.vue'
const Server = () => import('../views/serve/serve.vue')
const Bisai = () => import('../views/bisai/bisai.vue')
const Carerr = () => import('../views/carerr/carerr.vue')
const Display = () => import('../views/display/display.vue')
const Statistics = () => import('../views/Statistics-page/Statistics-page.vue')
const Login = () => import('@/components/login/login.vue')
const Register = () => import('@/views/register/register.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Login
  },
  {
    path: '/Bisai',
    component: Bisai,
    meta: {
      istrun: true,
      requireAuth: true
    }
  },
  {
    path: '/work',
    component: work,
    meta: {
      istrun: true,
      requireAuth: true
    }
  },
  {
    path: '/Register',
    component: Register
    // meta: {
    //   istrun: true,
    //   requireAuth: true
    // }
  },
  {
    path: '/Carerr',
    component: Carerr,
    meta: {
      istrun: true
    }
  },
  {
    path: '/Display',
    component: Display,
    meta: {
      istrun: true
    }
  },
  {
    path: '/Server',
    component: Server,
    meta: {
      istrun: true
    }
  },
  {
    path: '/Statistics',
    component: Statistics
  }

]

const router = new VueRouter({
  routes
})

export default router

router.beforeEach((to, from, next) => {
  if (to.matched.some((r) => r.meta.requireAuth)) {
    const user = (localStorage.getItem('user'))
    if (user) { // 判断是否已经登录
      // console.log('这是通过拦截后到处理', from)

      next()
    } else {
      next({
        path: '/Login',
        query: { redirect: to.fullPath } // 登录成功后重定向到当前页面
      })
    }
  } else {
    // console.log('这是拦截')
    next()
  }
  // 如果本地 存在 token 则 不允许直接跳转到 登录页面
  if (to.fullPath === '/Login') {
    if (localStorage.getItem('user')) {
      next({
        path: from.fullPath
      })
    } else {
      next()
    }
  }
})
